package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.SubType;
import ex.com.jm.food.entity.SupType;
import ex.com.jm.food.service.AdminSubTypeService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminSubTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private SubType subType;
	private AdminSubTypeService adminSubTypeService;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private Integer supId;
	private String subName;
	private Integer subId;
	private List<SupType> supTypes;
	private Integer supTypeId;
	public List<SupType> getSupTypes() {
		return supTypes;
	}
	public void setSupTypes(List<SupType> supTypes) {
		this.supTypes = supTypes;
	}
	public SubType getSubType() {
		return subType;
	}
	public void setSubType(SubType subType) {
		this.subType = subType;
	}
	public AdminSubTypeService getAdminSubTypeService() {
		return adminSubTypeService;
	}
	public void setAdminSubTypeService(AdminSubTypeService adminSubTypeService) {
		this.adminSubTypeService = adminSubTypeService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public Integer getSupId() {
		return supId;
	}
	public void setSupId(Integer supId) {
		this.supId = supId;
	}
	public String getSubName() {
		return subName;
	}
	public void setSubName(String subName) {
		this.subName = subName;
	}
	public Integer getSubId() {
		return subId;
	}
	public void setSubId(Integer subId) {
		this.subId = subId;
	}
	
	public Integer getSupTypeId() {
		return supTypeId;
	}
	public void setSupTypeId(Integer supTypeId) {
		this.supTypeId = supTypeId;
	}
	/**
	 * 达到大类列表页面
	 * @return
	 */
	public String subTypeList(){
		return "subTypeList";
	}
	/**
	 * 得到数据
	 */
	public void getSubTypes(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<SubType> subTypes=this.getAdminSubTypeService().getSubTypes(offset, pageRow, subName, supId);
		JSONArray json=JSONArray.fromObject(subTypes);
		totalCount=this.getAdminSubTypeService().subTypeCount(subName, supId);
		JSONObject jo=JsonUtils.buildJson(json, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取小类信息
	 * @return
	 */
	public String editSubTye(){
		supTypes=this.getAdminSubTypeService().getSupTypes();
		if(subId!=null){
			subType=this.getAdminSubTypeService().editSubType(subId);
			//System.out.println(subType.getSupId().getSupId()+"==="+subType.getSupId().getSupName());
			supId=subType.getSupId().getSupId();
		}
		return "edit";
	}
	/**
	 * 保存/更新信息
	 * @return
	 */
	public String saveSubType(){
		System.out.println("supTypeId===========>>>>>"+supTypeId);
		if(subType.getSubName()==null||subType.getSubName().equals("")){
			addFieldError("subType.subName", "小类信息不能为空");
			return "edit";
		}
		SubType checkName=this.getAdminSubTypeService().checkName(subType.getSubName());
		if(checkName!=null&&subType.getSubId()==null){
			addFieldError("subType.subName", "小类信息已存在");
			return "edit";
		}
		if(subType.getSubId()==null||subType.getSubId().equals("")){
			subType.setCreated(DateUtils.getCurrentDate());
		}
		
		subType.setSupId(this.getAdminSubTypeService().getSupType(supTypeId));
		if(subType.getSubId()!=null&&!subType.getSubId().equals("")){
			this.getAdminSubTypeService().updateSubType(subType);
			return "subTypeList";
		}
		this.getAdminSubTypeService().saveSubType(subType);
		return "subTypeList";
	}
	/**
	 * 删除小类信息
	 * @return
	 */
	public String delSubType(){
		this.getAdminSubTypeService().delSubType(subId);
		return "subTypeList";
	}
}
